﻿using System;
using MoonSharp.Interpreter;
using UnityEngine;

// Token: 0x020001FF RID: 511
public class LuaBehaviorInstance : IBehaviorInstance
{
	// Token: 0x06000AE4 RID: 2788 RVA: 0x0005C8D8 File Offset: 0x0005AAD8
	public LuaBehaviorInstance(Table i, Script s)
	{
		this.instance = i;
		this.script = s;
		this.start = this.GetMethod("Start");
		this.update = this.GetMethod("Update");
		this.lazyUpdate = this.GetMethod("LazyUpdate");
		this.fixedUpdate = this.GetMethod("FixedUpdate");
		if (this.update.IsNil() && this.fixedUpdate.IsNil() && this.lazyUpdate.IsNil())
		{
			this.autoFinish = true;
		}
		this.exit = this.GetMethod("Exit");
	}

	// Token: 0x06000AE5 RID: 2789 RVA: 0x0005C988 File Offset: 0x0005AB88
	private DynValue GetMethod(string methodName)
	{
		DynValue dynValue = this.instance.Get(methodName);
		if (dynValue.IsNil())
		{
			dynValue = this.instance.MetaTable.Get(methodName);
			if (dynValue.IsNil())
			{
				dynValue = this.instance.MetaTable.Get("__index").Table.MetaTable.Get(methodName);
			}
		}
		return dynValue;
	}

	// Token: 0x06000AE6 RID: 2790 RVA: 0x0005C9F1 File Offset: 0x0005ABF1
	public void Start()
	{
		this.RunMethod(this.start);
	}

	// Token: 0x06000AE7 RID: 2791 RVA: 0x0005CA00 File Offset: 0x0005AC00
	public void Update()
	{
		if (this.autoFinish)
		{
			return;
		}
		if (!this.update.IsNil())
		{
			this.RunMethod(this.update);
		}
		if (!this.lazyUpdate.IsNil())
		{
			this.RunMethod(this.lazyUpdate);
		}
	}

	// Token: 0x06000AE8 RID: 2792 RVA: 0x0005CA51 File Offset: 0x0005AC51
	public void FixedUpdate()
	{
		if (this.autoFinish || this.fixedUpdate.IsNil())
		{
			return;
		}
		this.RunMethod(this.fixedUpdate);
	}

	// Token: 0x06000AE9 RID: 2793 RVA: 0x0005CA7B File Offset: 0x0005AC7B
	public void Exit()
	{
		this.RunMethod(this.exit);
	}

	// Token: 0x06000AEA RID: 2794 RVA: 0x0005CA89 File Offset: 0x0005AC89
	public bool AutoFinish()
	{
		return this.autoFinish;
	}

	// Token: 0x06000AEB RID: 2795 RVA: 0x0005CA94 File Offset: 0x0005AC94
	private void RunMethod(DynValue method)
	{
		if (method.IsNil())
		{
			return;
		}
		try
		{
			this.script.Call(method, new object[]
			{
				this.instance
			});
		}
		catch (ScriptRuntimeException ex)
		{
			Debug.LogError(ex.DecoratedMessage);
		}
	}

	// Token: 0x04000FF2 RID: 4082
	public string behaviorFilename;

	// Token: 0x04000FF3 RID: 4083
	private bool autoFinish;

	// Token: 0x04000FF4 RID: 4084
	public Table instance;

	// Token: 0x04000FF5 RID: 4085
	public Script script;

	// Token: 0x04000FF6 RID: 4086
	private DynValue start;

	// Token: 0x04000FF7 RID: 4087
	private DynValue update;

	// Token: 0x04000FF8 RID: 4088
	private DynValue lazyUpdate;

	// Token: 0x04000FF9 RID: 4089
	private DynValue fixedUpdate;

	// Token: 0x04000FFA RID: 4090
	private DynValue exit;
}
